package com.katze.boot.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import java.security.Key;
import java.security.Security;

public class EncryptUtils {
    private static final Logger log = LoggerFactory.getLogger(EncryptUtils.class);

    //设置默认密匙
    private static final String DEFAULT_KEY = "defaultKey";
    private static EncryptUtils utils = null;
    //加密
    private static Cipher encryptCipher = null;
    //解密
    private static Cipher decryptCipher = null;

    private EncryptUtils(String... strKey)  {
        try {
//            Security.addProvider(new com.sun.crypto.provider.SunJCE());
            Key key = getKey(strKey.length == 1? strKey[0].getBytes():DEFAULT_KEY.getBytes());

            encryptCipher = Cipher.getInstance("DES");
            encryptCipher.init(Cipher.ENCRYPT_MODE, key);

            decryptCipher = Cipher.getInstance("DES");
            decryptCipher.init(Cipher.DECRYPT_MODE, key);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static EncryptUtils getInstance(String... key){
        Security.addProvider(java.security.Security.getProvider("SunJCE"));
        if (utils == null){
            utils = new EncryptUtils(key);
        }
        return utils;
    }

    private byte[] encrypt(byte[] arrB) {
        try {
            return encryptCipher.doFinal(arrB);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 加密
     * @param strIn 加密内容
     */
    public String encrypt(String strIn) {
        try {
            byte[] bytes = encrypt(strIn.getBytes());
            return bytes == null? "": byteArr2HexStr(bytes);
        } catch (Exception e){
            log.error("{} 加密异常", strIn, e);
            return "";
        }
    }

    private byte[] decrypt(byte[] arrB) {
        try {
            return decryptCipher.doFinal(arrB);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解密
     * @param strIn 解密内容
     */
    public String decrypt(String strIn) {
        try {
            byte[] bytes = decrypt(hexStr2ByteArr(strIn));
            return bytes == null? "": new String(bytes);
        } catch (Exception e) {
            log.error("{} 解密异常", strIn, e);
            return "";
        }
    }

    private static Key getKey(byte[] arrBTmp) {
        byte[] arrB = new byte[8];
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        return new javax.crypto.spec.SecretKeySpec(arrB, "DES");
    }

    private static String byteArr2HexStr(byte[] arrB) {
        int iLen = arrB.length;
        StringBuilder sb = new StringBuilder(iLen * 2);
        for (byte anArrB : arrB) {
            int intTmp = anArrB;
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }

    private static byte[] hexStr2ByteArr(String strIn) {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;

        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }

//    public static String MD5Encrypt(String username, String password) {
//        return new SimpleHash("md5", password, ByteSource.Util.bytes(username + password), 2).toHex();
//    }
}
